<cfif Not IsDefined("session.id")><!---not logged in--->
	<cflocation url="/ecada/index.cfm"/><!---redirect to home where they will be able to log in--->
</cfif>

<cfif Not IsDefined("g")><!--- make sure variabls are defined--->
	<cfset session.message = "Incorrect Parameters!">
	<cflocation url="ListGroups.cfm"/><!---if not redirect--->
</cfif>

<cfset groupID = g>

<!---get the owner ID of the group--->

<cfquery datasource="exp" name="a">

	SELECT ownerID
	FROM groups
	WHERE id = #groupID#

</cfquery>
<!---the current user can remove themselfs from a group
     the owner of a group can also remove users from the froup
     all other users cannot remove other users from other groups--->

<cfif session.id eq a.ownerID>
	<!---Disband group--->
	<cftransaction>
		<cfquery name="Group" datasource="exp">
		
			SELECT name
			FROM groups
			WHERE id = #groupID#
		
		</cfquery>
		<cfquery name="GroupIDs" datasource="exp">

			DELETE FROM groupusers
			WHERE GroupID = #groupID#

		</cfquery>
		<cfquery name="GroupIDs" datasource="exp">

			DELETE FROM groups
			WHERE id = #groupID#

		</cfquery> 
	</cftransaction>
	<cfset session.message = "Sucessfully disbanded: #Group.name#">
<cfelse>
	<cfset session.message = "You do not have permission to disband this group!">
</cfif>
<cflocation url="ListGroups.cfm">